﻿using System.Collections.Generic;
using System.Web.Mvc;
using Contrib.AppFabric.Models;
using Contrib.AppFabric.Services;
using Contrib.AppFabric.ViewModels;
using Orchard;
using Orchard.ContentManagement;
using Orchard.Environment.Configuration;
using Orchard.Localization;
using Orchard.Security;
using Orchard.UI.Admin;
using Orchard.UI.Notify;

namespace Contrib.AppFabric.Controllers {
    [Admin]
    public class AdminController : Controller {
        private readonly IAppFabricServices _appFabricServices;
        private readonly ShellSettings _shellSettings;

        public AdminController(
            IOrchardServices services,
            IAppFabricServices appFabricServices,
            ShellSettings shellSettings) {
            _appFabricServices = appFabricServices;
            _shellSettings = shellSettings;
            Services = services;
        }

        public IOrchardServices Services { get; set; }
        public Localizer T { get; set; }

        public ActionResult Index() {
            if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not allowed to manage appfabric")))
                return new HttpUnauthorizedResult();

            var settings = Services.WorkContext.CurrentSite.As<AppFabricSettingsPart>();

            var model = new IndexViewModel {
                SecondsToWaitInRetryCircle = settings.SecondsToWaitInRetryCircle,
                MaxiumumNumberOfErrorsUntilRetryCircle = settings.MaxiumumNumberOfErrorsUntilRetryCircle,
                CacheName = string.IsNullOrWhiteSpace(settings.CacheName) ? _shellSettings.Name : settings.CacheName
            };

            return View(model);
        }

        [HttpPost]
        public ActionResult Index(IndexViewModel viewModel) {
            if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not allowed to manage appfabric")))
                return new HttpUnauthorizedResult();

            if (ModelState.IsValid) {
                var settings = Services.WorkContext.CurrentSite.As<AppFabricSettingsPart>();
                settings.MaxiumumNumberOfErrorsUntilRetryCircle = viewModel.MaxiumumNumberOfErrorsUntilRetryCircle;
                settings.SecondsToWaitInRetryCircle = viewModel.SecondsToWaitInRetryCircle;
                settings.CacheName = string.IsNullOrWhiteSpace(viewModel.CacheName) ? _shellSettings.Name : viewModel.CacheName;

                Services.Notifier.Information(T("Appfabric Settings saved successfully."));
            }

            return View(viewModel);
        }

        public ActionResult FarmManagement() {
            if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not allowed to manage appfabric")))
                return new HttpUnauthorizedResult();

            FarmManagementViewModel viewModel = new FarmManagementViewModel();

            viewModel.RegionLevelCaches = _appFabricServices.GetRegionLevelCaches();
            
            return View(viewModel);
        }

        public ActionResult RemoteFarmManagement() {
            if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not allowed to manage appfabric")))
                return new HttpUnauthorizedResult();

            RemoteFarmManagementViewModel viewModel = new RemoteFarmManagementViewModel();

            return View(viewModel);
        }

        [HttpPost]
        public ActionResult RemoteFarmManagement(RemoteFarmManagementViewModel viewModel) {
            return View(viewModel);
        }
    }
}